import pymysql


class DBTools:
    @staticmethod
    def create_connect():
        conn = pymysql.connect(host="211.103.136.244", port=7061, user="student",
                               password="iHRM_student_2022", database="ihrm", charset="utf8")
        return conn

    @staticmethod
    def query_one(sql):
        conn = None
        cursor = None
        ret = None
        try:
            conn = DBTools.create_connect()
            cursor = conn.cursor()
            cursor.execute(sql)
            ret = cursor.fetchone()
        except  Exception as e:
            print("出错:", e)
            raise
        finally:
            cursor.close()
            conn.close()
            return ret

    @staticmethod
    def query_all(sql):
        conn = None
        cursor = None
        ret = None
        try:
            conn = DBTools.create_connect()
            cursor = conn.cursor()
            cursor.execute(sql)
            ret = cursor.fetchall()
        except  Exception as e:
            print("出错:", e)
            raise
        finally:
            cursor.close()
            conn.close()
            return ret

    @staticmethod
    def query_many(sql, idx, n):
        conn = None
        cursor = None
        ret = None
        try:
            conn = DBTools.create_connect()
            cursor = conn.cursor()
            cursor.execute(sql)
            cursor.rownumber = idx - 1
            ret = cursor.fetchmany(n)
        except Exception as e:
            print("出错:", e)
            raise
        finally:
            cursor.close()
            conn.close()
            return ret

    @staticmethod
    def iud(sql):
        conn = None
        cursor = None
        ret = None
        try:
            conn = DBTools.create_connect()
            cursor = conn.cursor()
            ret = cursor.execute(sql)
            conn.commit()
            print("影响的行数:",ret)
        except  Exception as e:
            print("出错:", e)
            raise
        finally:
            cursor.close()
            conn.close()


if __name__ == '__main__':
    print(DBTools.query_one("DELETE FROM bs_user WHERE mobile ='18942190529';"))

